<?php
/**
 * CBC 암호화 복호화
 *
 * @package     util
 * @subpackage  des
 * @author      ulariul jung
 */

class utilDesCbc extends utilDesAbstract
{

    public static function getInstance($sEncryptKey, $sIv)
    {
        return new self($sEncryptKey, $sIv);
    }


    public function __construct($sEncrypt, $sIv)
    {
        $this->sEncryptKey      = $sEncrypt;
        $this->sIv              = $sIv;
        $this->sEncryptType     = MCRYPT_RIJNDAEL_128;
        $this->sEncryptMode     = MCRYPT_MODE_CBC;
    }

    /**
     * 암호화
     *
     * @param string $sString
     * @return string
     */
    public function encrypt($sString)
    {
        return trim(
                    bin2hex(
                        mcrypt_encrypt(
                            $this->sEncryptType,
                            $this->sEncryptKey,
                            $sString,
                            $this->sEncryptMode,
                            $this->sIv
                        )
                    )
               );
    }


    /**
     * 복호화
     *
     * @param string $sString
     * @return string | false
     */
    public function decrypt($sString)
    {
        if (empty($sString)) return false;

        return rtrim(
                    mcrypt_decrypt(
                            $this->sEncryptType,
                            $this->sEncryptKey,
                            pack("H*",$sString),
                            $this->sEncryptMode,
                            $this->sIv
                    ),
                    "\0\4"
               );
    }
}